package com.eightbears.beboy.ui.user.activity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.eightbears.beboy.R;
import com.eightbears.beboy.adapter.SearchHistoryAdapter;
import com.eightbears.beboy.base.BaseActivity;
import com.eightbears.beboy.base.cache.SearchKeyCache;
import com.eightbears.beboy.base.util.InputMethodUtils;
import com.eightbears.beboy.base.util.ShowToast;
import com.eightbears.beboy.base.view.SwipeBackLayout;
import com.eightbears.beboy.base.view.dialog.DefaultDialog;
import com.eightbears.beboy.base.view.layout.MyLinearLayoutManager;
import com.eightbears.greendao.SearchKey;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.eightbears.beboy.base.util.CommonAPI.SEARCH_KEY_POST;

/**
 * 主页搜索结果页面
 */
public class SearchPostActivity extends BaseActivity {

    public static final String PARAMS_SEARCH_KEY = "searchKey";

    @BindView(R.id.rv_list)
    RecyclerView rv_list;
    @BindView(R.id.et_search_shop)
    EditText et_query_shop;
    @BindView(R.id.sw_finish)
    SwipeBackLayout sw_finish;

    private List<SearchKey> list;
    private SearchKey searchKey;
    private SearchHistoryAdapter mSearchHistoryAdapter;

    private int mPosition;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
        initView();
        initData();
        initEvent();
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        //侧滑删除实力化
        et_query_shop.setHint(R.string.text_post_search);
        rv_list.setLayoutManager(new MyLinearLayoutManager(this));
        mSearchHistoryAdapter = new SearchHistoryAdapter();
        mSearchHistoryAdapter.openLoadAnimation();
        rv_list.setAdapter(mSearchHistoryAdapter);
    }

    @Override
    protected void initData() {
        list = SearchKeyCache.getInstance(this).getCacheByType(SEARCH_KEY_POST);
        mSearchHistoryAdapter.setNewData(list);
    }

    @Override
    protected void initEvent() {
        mSearchHistoryAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                searchKey = (SearchKey) adapter.getData().get(position);
                mPosition = position;
                switch (view.getId()) {
                    case R.id.tv_search_title:
                        startLocalActivity(SearchPostActivity.this, SearchPostListActivity.class, PARAMS_SEARCH_KEY, searchKey);
                        break;
                    case R.id.ll_del_item:
                        deleteItem();
                        break;
                }

            }
        });
        //侧滑
        sw_finish.setCallBack(new SwipeBackLayout.CallBack() {
            @Override
            public void onFinish() {
                InputMethodUtils.hindInputMethod(getApplication());
                finish();
            }
        });
    }

    /**
     * 退出
     */
    @OnClick(R.id.ll_back)
    void back() {
        InputMethodUtils.closeInputMethod(SearchPostActivity.this, et_query_shop);
        finish();
    }

    /**
     * 搜索
     */
    @OnClick(R.id.tv_search)
    void search() {
        String searchText = et_query_shop.getText().toString().trim();
        if (searchText.equals("")) {
            ShowToast.showShortToast(getString(R.string.text_search_text_empty));
            return;
        }
        SearchKeyCache.getInstance(this).saveData(new SearchKey(SEARCH_KEY_POST, searchText));
        //刷新列表
        list = SearchKeyCache.getInstance(this).getCacheByType(SEARCH_KEY_POST);
        mSearchHistoryAdapter.setNewData(list);
        mSearchHistoryAdapter.notifyDataSetChanged();
        startLocalActivity(this, SearchPostListActivity.class, PARAMS_SEARCH_KEY, new SearchKey(SEARCH_KEY_POST, searchText));
    }

    /**
     * 删除
     */
    @OnClick(R.id.iv_delete)
    void deleteAll() {
        //弹出对话框
        DefaultDialog.Builder defaultDialog = new DefaultDialog.Builder(mContext);
        defaultDialog.setTitle(getString(R.string.alert_text));
        defaultDialog.setMessage(getString(R.string.alert_delete_search_all));

        defaultDialog.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                //删除数据库
                SearchKeyCache.getInstance(mContext).clearAllData();
                mSearchHistoryAdapter.getData().clear();
                mSearchHistoryAdapter.notifyDataSetChanged();
                dialog.dismiss();
            }
        });
        defaultDialog.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        defaultDialog.create().show();
    }

    /**
     * 删除item
     */
    void deleteItem() {
        //弹出对话框
        DefaultDialog.Builder defaultDialog = new DefaultDialog.Builder(mContext);
        defaultDialog.setTitle(getString(R.string.alert_text));
        defaultDialog.setMessage(getString(R.string.alert_delete_search_item));

        defaultDialog.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                //删除数据库
                SearchKeyCache.getInstance(getApplication()).deleteItemByName(searchKey.getSearch_title(), SEARCH_KEY_POST);
                mSearchHistoryAdapter.remove(mPosition);
                mSearchHistoryAdapter.notifyDataSetChanged();
                dialog.dismiss();
            }
        });
        defaultDialog.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        defaultDialog.create().show();
    }
}